/* Hawk - An OSGI Based Test Framework
 *
 * Copyright (C) 2010 http://code.google.com/p/hawk-framework/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package org.hawk.core.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * This is a utility class used to read and write files
 */
public class FileIO
{
	/**
	 * This method is able do write a file into the hard disk
	 * 
	 * @param A file name
	 * @param The file content
	 * @throws IOException 
	 */
	public static void writeFile(String fileName, String content) {	
		try {
			File outFile = new File(fileName);
			FileWriter fileWriter = new FileWriter(outFile);
			
			fileWriter.write(content);
			fileWriter.close();
		}
		catch (IOException e) {	
			e.printStackTrace();
		}
	}
	
	/**
	 * This method is able do read files
	 * 
	 * @param  A file name
	 * @return A file in string format
	 */
    public static String readFile(String fileName)
    {
        String returnValue = null;
        try
        {
            BufferedReader br = new BufferedReader(new FileReader(fileName) );
            StringBuffer sb = new StringBuffer();
            String temp = br.readLine();
            while(temp != null)
            {
                sb.append(temp);
                sb.append("\n");
                temp = br.readLine();
            }
            returnValue = sb.toString();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return returnValue;
    }
}